﻿using AutoMapper;
using housingsafety.server.Entities;
using housingsafety.server.Entities.DataModels;
using housingsafety.server.Helpers;
using housingsafety.server.Models;
using housingsafety.server.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using housingsafety.server.Svcs;
using Microsoft.AspNetCore.Http;
using System.Net.Http.Headers;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using housingsafety.server.Security;
using System.Text;
using Microsoft.EntityFrameworkCore;
using Dapper;

namespace housingsafety.server.Controllers
{
    /// <summary>
    /// 用户偏好设置(Ok)
    /// </summary>
    public class UserController : BaseController<UserController>
    {
        private const string AvatarGroupName = "avatars";
        private readonly string[] _pictureFormatArray = { ".png", ".jpg", ".jpeg", ".PNG", ".JPG", ".JPEG" };

        private readonly IRepository<ZmUser> _userRepository;
        private readonly ZmDbContext _dbContext;
        private readonly IMapper _mapper;

        public UserController(IRepository<ZmUser> userRepository
            , ZmDbContext dbContext
            , IMapper mapper
            , ILogger<UserController> logger
            , IRepository<ZmSystemLog> systemLogRepository) : base(logger, systemLogRepository)
        {
            _mapper = mapper;
            _userRepository = userRepository;
            _dbContext = dbContext;
        }

        /// <summary>
        /// 编辑个人信息
        /// </summary>
        /// <param name="userRegisterInput">个人信息</param>
        /// <returns>是否成功</returns>
        [HttpPost("preference/profile/edit")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet), 200)]
        public RespRet Edit([FromBody] UserInfoInput userRegisterInput)
        {
            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet(false, "no logined user");
            }

            var count = _userRepository.Count(u => string.Equals(u.Nickname, userRegisterInput.Nickname)
            && u.IsDelete != 1);
            if (count > 0)
            {
                return new RespRet(false, "nickname is repeat");
            }

            var firstOrDefault = _userRepository.FirstOrDefault(u => u.Number == userNumber);
            if (null != firstOrDefault)
            {
                firstOrDefault.Nickname = userRegisterInput.Nickname;
                firstOrDefault.Email = userRegisterInput.Email;
                firstOrDefault.ModifiedIp = this.IpAddress();
                firstOrDefault.ModifiedOn = DateTime.Now;
                _userRepository.Update(firstOrDefault);
                _userRepository.SaveChanges();

                return new RespRet(true, "Ok");
            }

            return new RespRet(false, "user not matched");
        }

        [HttpGet("preference/profile/info")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<UserDto>), 200)]
        public RespRet<UserDto> Profile()
        {
            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<UserDto>(false, "no logined user");
            }

            var data = (from user in _dbContext.ZmUser
                        where user.Number == userNumber
                        select _mapper.Map<UserDto>(user)).FirstOrDefault();
            if (null != data)
            {
                if (data.JurisdictionCodes != null && data.JurisdictionCodes.Count > 0)
                {
                    data.JurisdictionNames = data.JurisdictionCodes.Select(_ => _.RegionName).ToList();
                }

                if (data.Role == PermissionRoleType.UnitAdmin || data.Role == PermissionRoleType.UnitInvestigator)
                {
                   
                }
                return new RespRet<UserDto>(true, "Ok", data);
            }

            return new RespRet<UserDto>(false, "user not matched");
        }
    }
}
